package cst.a000wxb.fragment115wxb;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cst.a000wxb.fragment115wxb.fragment.WechatFragment;
import cst.a000wxb.fragment115wxb.fragment.WechatFriendFragment;
import cst.a000wxb.fragment115wxb.fragment.WechatMEFragment;
import cst.a000wxb.fragment115wxb.fragment.WechatSETFragment;


public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private LinearLayout linearLayout1,linearLayout2,linearLayout3,linearLayout4;
    private ImageButton imageButton1,imageButton2,imageButton3,imageButton4;
    private List<Fragment> listFragment;
    private FragmentPagerAdapter fragmentPagerAdapter;


    View.OnClickListener  onClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            resetImage();
            switch (v.getId()){
                case R.id.layout1:
                    Toast.makeText(MainActivity.this,"first",Toast.LENGTH_SHORT).show();
                    selectTab(0);
                    break;
                case R.id.layout2:
                    Toast.makeText(MainActivity.this,"first",Toast.LENGTH_SHORT).show();
                    selectTab(1);
                    break;
                case R.id.layout3:
                    Toast.makeText(MainActivity.this,"first",Toast.LENGTH_SHORT).show();
                    selectTab(2);
                    break;
                case R.id.layout4:
                    Toast.makeText(MainActivity.this,"first",Toast.LENGTH_SHORT).show();
                    selectTab(3);
                    break;
            }

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initFragment();
    }

    private void resetImage(){
        imageButton1.setImageResource(R.drawable.weixin_normal);
        imageButton2.setImageResource(R.drawable.friend_normal);
        imageButton3.setImageResource(R.drawable.address_normal);
        imageButton4.setImageResource(R.drawable.settings_normal);
    }

    private void selectTab(int i){
        switch (i){
            case 0:imageButton1.setImageResource(R.drawable.weixin_pressed);
                break;
            case 1:imageButton2.setImageResource(R.drawable.friend_pressed);
                break;
            case 2:imageButton3.setImageResource(R.drawable.address_pressed);
                break;
            case 3:imageButton4.setImageResource(R.drawable.settings_pressed);
                break;
        }
        viewPager.setCurrentItem(i);

    }


    private void init() {
        linearLayout1 = (LinearLayout)findViewById(R.id.layout1);
        linearLayout2 = (LinearLayout)findViewById(R.id.layout2);
        linearLayout3 = (LinearLayout)findViewById(R.id.layout3);
        linearLayout4 = (LinearLayout)findViewById(R.id.layout4);

        imageButton1 = (ImageButton)findViewById(R.id.imageButton1);
        imageButton2 = (ImageButton)findViewById(R.id.imageButton2);
        imageButton3 = (ImageButton)findViewById(R.id.imageButton3);
        imageButton4 = (ImageButton)findViewById(R.id.imageButton4);

        linearLayout1.setOnClickListener(onClick);
        linearLayout2.setOnClickListener(onClick);
        linearLayout3.setOnClickListener(onClick);
        linearLayout4.setOnClickListener(onClick);

        imageButton1.setOnClickListener(onClick);
        imageButton2.setOnClickListener(onClick);
        imageButton3.setOnClickListener(onClick);
        imageButton4.setOnClickListener(onClick);

        viewPager = (ViewPager)findViewById(R.id.viewPager);
    }
    private void initFragment(){
        listFragment = new ArrayList<Fragment>();
        listFragment.add(new WechatFragment());
        listFragment.add(new WechatFriendFragment());
        listFragment.add(new WechatMEFragment());
        listFragment.add(new WechatSETFragment());

        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return listFragment.get(position);
            }

            @Override
            public int getCount() {
                return listFragment.size();
            }
        };
        viewPager = (ViewPager)findViewById(R.id.viewPager);
        viewPager.setAdapter(fragmentPagerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                viewPager.setCurrentItem(position);
                resetImage();
                selectTab(position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
